package com.aptoto.demo;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;

import com.aptoto.demo.config.DispatcherServletInitializer;
import com.aptoto.demo.config.WebConfig;




@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/*")
                    .allowedHeaders("*")
                    .allowedMethods("*")
                    .allowedOrigins("*");
            }
        };
    }
	@Bean
    public FilterRegistrationBean corssDomainFilterReg() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new CrossDomainAllowFiler());
        registration.addUrlPatterns("/*");
        registration.setName("CrossDomainAllowFiler");
        registration.setOrder(2);
        return registration;
    }
	 static class CrossDomainAllowFiler implements Filter {

        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
            HttpServletResponse httpResponse = (HttpServletResponse)response;
            // 表明它允许"http://xxx"发起跨域请求
            httpResponse.setHeader("Access-Control-Allow-Origin","*");
            // 表明在xxx秒内，不需要再发送预检验请求，可以缓存该结果
            httpResponse.setHeader("Access-Control-Allow-Methods","POST, GET, DELETE, PUT");
            // 表明它允许xxx的外域请求
            httpResponse.setHeader("Access-Control-Max-Age","3628800");
            // 表明它允许跨域请求包含xxx头
            httpResponse.setHeader("Access-Control-Allow-Headers","x-requested-with");
            httpResponse.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
            chain.doFilter(request, response);
        }

        @Override
        public void destroy() {
            // TODO Auto-generated method stub
            
        }
        
    }
}
